package com.meiyu.live.tv.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.meiyu.live.R;
import com.meiyu.live.popj.Assort;
import com.meiyu.live.ui.columnfollow.ColumnFollowView;
import com.meiyu.live.util.DisplayUtils;
import com.meiyu.live.widget.XCFlowLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * 开始直播，标签选择对话框
 */
public class LabelSelectDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private ViewGroup.MarginLayoutParams lp;

    private View btn_ok;
    private XCFlowLayout flowlayout_label;

    private List<Assort> labelList = new ArrayList<>();
    private LabelSelectedListener listener;

    public interface LabelSelectedListener {
        void onAssortSelected(List<Assort> selectedList);
    }

    public LabelSelectDialog(Context context) {
        super(context, R.style.dialog_custom);
        init(context);
    }

    public LabelSelectDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        setContentView(R.layout.dialog_labelselect);
        setCanceledOnTouchOutside(false);
        lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = DisplayUtils.dpToPixel(context, 5);
        lp.rightMargin = lp.leftMargin;
        lp.topMargin = lp.rightMargin;
        lp.bottomMargin = lp.topMargin;

        btn_ok = findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(this);
        flowlayout_label = (XCFlowLayout) findViewById(R.id.flowlayout_label);
    }

    public void setListener(LabelSelectedListener listener) {
        this.listener = listener;
    }

    public void setData(List<Assort> list) {
        if (list != null && list.size() > 0) {
            this.labelList.clear();
            this.labelList.addAll(list);
            update();
        }
    }

    private void update() {
        flowlayout_label.removeAllViews();
        for (Assort assort : labelList) {
            final ColumnFollowView followView = new ColumnFollowView(getContext());
            followView.setOnClickListener(onClickListener);
            followView.setData(assort);
            followView.update();
            flowlayout_label.addView(followView, lp);
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ColumnFollowView followView = (ColumnFollowView) view;
            followView.getData().isSelected = !followView.getData().isSelected;
            followView.update();
        }
    };

    @Override
    public void onClick(View view) {
        if (view == btn_ok) {
            if (listener != null) {
                List<Assort> selectList = new ArrayList<>();
                for (Assort assort : labelList) {
                    if (assort.isSelected) {
                        selectList.add(assort);
                    }
                }
                listener.onAssortSelected(selectList);
            }
            dismiss();
        }
    }
}
